package chai.ning.inner_class.train_15;

import java.time.LocalDate;
import java.util.Arrays;
import java.util.Comparator;
import java.util.Random;

public class CustomerTest {

	public static void main( String[] args ) {
        
        final Random random = new Random();
        
        final Customer[] customers = new Customer[ 9 ] ;
        
        char[] gen = {'男','女'};
        char[] sur = {'李','赵','付','史','柴','张','王','罗','缑'};
        char[] nam = {'仁','义','礼','智','信','温','良','恭','俭','让','忠','孝','勇','恭','廉'};
        char[] tna = { '枚','兰','竹','鞠','启','玉','轩','昂','隆','羽','凡','柯'};
        // 使用循环对 customers 数组进行初始化
        for( int i = 0 ; i < customers.length ; i++){
            // 随机产生 年份、月份、日期，并将据此创建 LocalDate 对象中
            int y = random.nextInt(50)+1970;
            int m = random.nextInt(12);
            int d = random.nextInt(12)+18;

            LocalDate dates = LocalDate.of(y, m, d);
            
            // 在这里创建 Customer 对象
            customers[ i ] = new Customer();
            customers[ i ].setId( i );
            customers[ i ].setGender( gen[ random.nextInt( 2 ) ] ) ;
            customers[ i ].setName( "" + sur[ random.nextInt(9)] + nam[ random.nextInt(15)] + tna[ random.nextInt(12)]);
            customers[ i ].setBirthdate( dates);
        }
        
        // 使用循环输出排序前的 customers
        for(int i= 0;i<customers.length ;i++) {
        	System.out.println( "Id:" + customers[i].getId() + " " + "性別: " + customers[i].getGender() + " " + "姓名: " + customers[i].getName() + " " + "出生: " + customers[i].getBirthdate());
        }
        // 使用 【匿名类】实现 比较器接口 ，以便于按照 Customer 的 birthdate 升序排序
        Comparator<Customer> comparator = new Comparator<Customer>(){

			@Override
			public int compare(Customer o1, Customer o2) {

				if( o1.getBirthdate().isBefore(o2.getBirthdate()) ) {
					return -1;
				}else if( o1.getBirthdate().isBefore(o2.getBirthdate()) ){
					return 1;
				}else {
				return 0;
				}
			}
        	
        };
        
        Arrays.sort( customers , comparator ); // 用比较器排序
        
        // 使用循环输出排序后的日期
        System.out.println();
        
        for(int i= 0;i<customers.length ;i++) {
        	System.out.println( "Id:" + customers[i].getId() + " " + "性別: " + customers[i].getGender() + " " + "姓名: " + customers[i].getName() + " " + "出生: " + customers[i].getBirthdate());
        }
    }

}